<template>
  <div class="role">
    <pageSearch
      :searchFormConfig="searchFormConfig"
      @queryBtnClick="queryBtnClick"
      @resetBtnClick="resetBtnClick"
    />
    <pageContent
      ref="pageContentRef"
      :contentTableConfig="contentTableConfig"
      pageName="role"
      title="新增角色"
      @newBtnClick="newBtnClick"
      @editBtnClick="editBtnClick"
    ></pageContent>
    <page-model
      :modelConfig="modelConfig"
      :title="modelTitle"
      pageName="role"
      ref="pageModelRef"
      :defaultInfo="defaultInfo"
      :otherInfo="otherInfo"
    >
      <div class="menu-tree">
        <el-tree
          ref="elTreeRef"
          :data="menus"
          show-checkbox
          node-key="id"
          @check="checkChangeHandle"
          :props="{
            children: 'children',
            label: 'name'
          }"
        />
      </div>
    </page-model>
  </div>
</template>

<script lang="ts">
import { computed, defineComponent, ref, nextTick } from 'vue'
import { useStore } from '@/store'
import pageSearch from '@/components/page-search'
import pageContent from '@/components/page-content'
import pageModel from '@/components/page-model'

import { searchFormConfig } from './config/search.config'
import { contentTableConfig } from './config/content.config'
import { modelConfig } from './config/model.config'

import { usePageSearch } from '@/hooks/usePageSearch'
import { usePageModel } from '@/hooks/usePageModel'

import { getMenuLeafKeys } from '@/utils/map-menus'
import { ElTree } from 'element-plus'

export default defineComponent({
  name: 'role',
  components: {
    pageContent,
    pageSearch,
    pageModel
  },
  setup() {
    // 封装searchpage中的逻辑封装
    const { pageContentRef, queryBtnClick, resetBtnClick } = usePageSearch()

    // 封装pageModel中的操作逻辑的hooks
    const elTreeRef = ref<InstanceType<typeof ElTree>>()
    const editBtnHandle = (item: any) => {
      const defaultKeys = getMenuLeafKeys(item.menuList)

      nextTick(() => {
        elTreeRef.value?.setCheckedKeys(defaultKeys, false)
      })
    }
    const { pageModelRef, defaultInfo, newBtnClick, editBtnClick } =
      usePageModel(() => {}, editBtnHandle)

    const modelTitle = computed(() => {
      return Object.keys(defaultInfo.value).length > 0 ? '编辑角色' : '新增角色'
    })

    const store = useStore()
    const menus = computed(() => {
      return store.state.entireMenu
    })

    const otherInfo = ref({})
    const checkChangeHandle = (currentNode: any, nodes: any) => {
      const { checkedKeys, halfCheckedKeys } = nodes
      otherInfo.value = { menuList: [...checkedKeys, ...halfCheckedKeys] }
    }

    return {
      contentTableConfig,
      searchFormConfig,
      modelConfig,
      pageContentRef,
      queryBtnClick,
      resetBtnClick,
      newBtnClick,
      pageModelRef,
      defaultInfo,
      editBtnClick,
      modelTitle,
      menus,
      checkChangeHandle,
      otherInfo,
      elTreeRef
    }
  }
})
</script>

<style scoped lang="less">
.menu-tree {
  margin-left: 25px;
}
</style>
